<?php
include_once('saet.ex.class.php');
include_once('config/config.php');
require_once(ABSPATH. 'include/control/baseControl.php');
require_once(ABSPATH. 'include/model/Post.php');
require_once(ABSPATH. 'include/model/Reply.php');
require_once(ABSPATH. 'include/dao/PostDAO.php');
require_once(ABSPATH. 'include/dao/ReplyDAO.php');

class JobControl extends baseControl{
	public $job ;
	public $replies;
	
	private $dao ; 
	private $usr;
	
	function __construct($current_usr){
		$this-> dao = new PostDAO();	
		$this -> usr = 	$current_usr;
		parent::__construct();
    }
    
    function get(){
    	$id = $_GET["id"];  
    	if(!!!$id)
    		return;
    		
    	$replyDao=new ReplyDAO();	  	   	
    	$this-> job = $this-> dao->get($id);
    	$this-> replies =$replyDao -> get_postId($id,0);
    }
    
    function post(){
    	if(!$this->usr){
    		header("Location: /");
    	}
    	
    	$p = new Post();
    	$p-> category_id = $_POST["category_id"];
    	$p-> user_id = $this -> usr -> id ;
    	$p-> title =  $_POST["title"];
    	$p-> body = $_POST["body"];
    	$p-> post_date = date('Y-m-d h:i:s');
    	$p-> last_update_date = date('Y-m-d h:i:s');
    	$p-> last_reply_date = date('Y-m-d h:i:s');    	
    	$p-> keywords="";
    	$p-> description="";    	
    	
    	$this-> dao->insert(&$p);     	
    	$this->Model = $p;
     	
    	$tStauts = mb_substr($p->title . str_replace('\r\n','',$p-> body),0,120,'utf-8') .'http://itjobs.sinaapp.com/job/'.$p->id;
    	if($_POST["needSyn"]){
	    	$tclient = new SaeTClient(WB_AKEY,WB_SKEY,
			$this->usr->access_token,
			$this->usr->access_token_secret);
			$tclient->update($tStauts);
    	}
    	
    	if($p->id)
    		header("Location: /job/".$p->id);
    }
    
    function put(){
    	if(!$this->usr){
    		header("Location: /");
    	}
    	
    	$id = $_POST["id"];  
    	$p = $this-> dao->get($id);   
    	
    	 	
    	$p-> title =  $_POST["title"];
    	$p-> body = $_POST["body"];
    	$p-> category_id = $_POST["category_id"];
    	$p-> keywords="";
    	$p-> description="";
    		
    	
    	$this-> dao -> update($p);  
    	header("Location: /job/$id");
    }

    function delete(){
    	if(!$this->usr){
    		header("Location: /");
    	}
    	
    	echo "delete";
    }
}
?>